/**
 * 区域管理的JS方法
 */
XF.Area = new function(){
	
	this.init = function(areaId,siteId,siteName){
		_init_combobox(areaId,siteId,siteName);
	};
	
	/**
	 * 新增区域
	 */
	this.addArea = function(){
		XF.lightSideBar("addArea");
		var url  = global.contextPath + "/area/add"
		$.ajax({
		  url: url,
		  type: "post",
		  data: {},
		  complete: function(xhr){
			  xhr = xhr['responseText'];
			  $("#areaCenter").html(xhr);
		  }
		});
	};
	
	/**
	 * 区域管理
	 */
	this.listArea = function(){
		XF.lightSideBar("listArea");
		_area_search(null,null,null,null,null,null,null,null);
	}
	
	/**
	 * 通用调用方法
	 */
	this.listAreaSearch = function(orderByFieldReq){
		var orderByField = null;
		var orderByMode = null;
		
		var siteId = null;
		var siteName = null;
		var areaId = null;
		var areaName = null;
		var fldidd = null;
		var fldsitenames = null;
		
		siteId = $("input[name=siteId]").val();
		siteName = $("input[name=siteName]").val();
		areaId = $("input[name=areaId]").val();
		areaName = $("input[name=markName]").val();
		fldidd = $("input[name=fldidd]").val();
		fldsitenames = $("input[name=fldsitenames]").val();
		
		if(typeof(orderByFieldReq)!='undefined' && orderByFieldReq!=""){
			orderByField = orderByFieldReq;
			var td = $("th[orderByField="+orderByField+"]");
			orderByMode = td.find("span.px").attr("orderByMode");
		}
		_area_search(orderByField,orderByMode,siteId,siteName,areaId,areaName,fldidd,fldsitenames);
	};
	
	/**
	 * 编辑
	 */
	this.editArea = function(id){
		var url  = global.contextPath + "/area/edit"
		$.ajax({
		  url: url,
		  type: "post",
		  data: {fldidd:id},
		  complete: function(xhr){
			  xhr = xhr['responseText'];
			  $("#areaCenter").html(xhr);
		  }
		});
	};
	
	this.EditCoordinate = function(id,name){
		var url  = global.contextPath + "/area/editCoordinate"
		$.ajax({
		  url: url,
		  type: "post",
		  data: {areaId:id,areaName:name},
		  complete: function(xhr){
			  xhr = xhr['responseText'];
			  $("#areaCenter").html(xhr);
		  }
		});
	};
	
	this.EditCoordinatePopup = function(id,name){
		var url  = global.contextPath + "/area/editCoordinatePopup"
		$.ajax({
		  url: url,
		  type: "post",
		  data: {areaId:id,areaName:name},
		  complete: function(xhr){
			  xhr = xhr['responseText'];
			  $("#coordinate-popup").remove();
			  $("body").append(xhr);
			  var win;
			  var width=950;
			  var height=650;
			  win = $('#coordinate-popup').window({
			      title: '编辑区域坐标',
			      width: width,
			      height: height,
			      top: ($(window).height() - height) * 0.5,
			      left: ($(window).width() - width) * 0.5,
			      shadow: true,
			      modal: true,
			      iconCls: 'icon-add',
			      closed: true,
			      minimizable: false,
			      maximizable: false,
			      collapsible: false
			  });
			  win.window('open');
			  XF.Area.coordinateWindow = {};
			  XF.Area.coordinateWindow = win;
		  	}
		});
	};
	
	this.deleteArea = function(ofAreaId,fldidd){
		$.messager.confirm('操作提示','确定删除这条记录吗',function(data){
			if(data){
				$.ajax({
					url:""+global.contextPath+"/area/doDeleteArea",
					type:"post",
					data:{areaId:ofAreaId,fldidd:fldidd},
					success:function(data){
						$.messager.show({
							title:'提示',
							msg:'删除成功',
							timeout:3000,
							showType:'slide'
						});
						XF.Area.listArea();
					}
				});
			}
		});	
	}
	
	/**
	 * 关闭弹出层
	 */
	this.closePopup = function(){
		$("#coordinate-popup").window('close', true);
	}
	
	/**
	 * 添加编辑失败错误提示
	 */
	this.refreshAfterAddFaild = function(errorJson){
		if(typeof(errorJson)=="undefined")return;
		if(errorJson==null || errorJson=="")return;
		$("span[id*='errorTip']").hide();
		for(key in errorJson){
			var v = errorJson[key];
			var errorTip = $("#errorTip_"+key);
			errorTip.html(v);
			errorTip.show();
		}
	};
    
    /**
     * 具体搜索方法实现
     */
    function _area_search(orderByField,orderByMode,siteId,siteName,areaId,areaName,fldidd,fldsitenames){	    
    	config.area_search['args'] = [orderByField,orderByMode,siteId,siteName,areaId,areaName,fldidd,fldsitenames];
    	SMT.xconfig.gvar.pageNum = '1';
    	SMT.ajax(config.area_search);
    }
    
    /**
     * 初始化combobox
     */
    function _init_combobox(areaId,siteId,siteName){
        $('#areaNameList').combobox({
            url:global.contextPath + "/area/areaList",
            valueField:'id',
            textField:'text',
            mode:'remote',
        	filter: function(q, row){
        		var opts = $(this).combobox('options');
        		return row[opts.textField].indexOf(q) == 0;
        	},
        	onSelect:function(record) {
        		var v = record.id;
        		$.ajax({
        			url:global.contextPath + "/area/querySite",
        			data:{areaId:v},
        			complete:function(xhr){
        				if(typeof(xhr)=="undefined" || typeof(xhr['responseText'])=="undefined" || xhr['responseText'] == ""){
            				$("input[name=siteId]").val("");
            				$("input[name=siteName]").val("");
        				}
        				xhr = xhr['responseText'];
        				xhr = new Function("return "+xhr);
        				xhr = xhr();
        				var siteId = xhr['siteId'];
        				var siteName = xhr['siteName'];
        				$("input[name=siteId]").val(siteId);
        				$("input[name=siteName]").val(siteName);
        			}
        		});
        	}
        });
        if(typeof(areaId)!='undefined' && typeof(siteId)!='undefined' && typeof(siteName)!='undefined' && areaId!=""){
            $('#areaNameList').combobox('setValue', areaId);
        	$("input[name=siteId]").val(siteId);
        	$("input[name=siteName]").val(siteName);
        }
    }
    
    this._querySite = function(){
		var v = $('#areaNameList').combobox('getValue');
		$.ajax({
			url:global.contextPath + "/area/querySite",
			data:{areaId:v},
			complete:function(xhr){
				if(typeof(xhr)=="undefined" || typeof(xhr['responseText'])=="undefined" || xhr['responseText'] == ""){
    				$("input[name=siteId]").val("");
    				$("input[name=siteName]").val("");
				}
				xhr = xhr['responseText'];
				xhr = new Function("return "+xhr);
				xhr = xhr();
				var siteId = xhr['siteId'];
				var siteName = xhr['siteName'];
				$("input[name=siteId]").val(siteId);
				$("input[name=siteName]").val(siteName);
			}
		});
    }

    /************************/
  //登记自动补全
	this.bigAutocomp = function(sid) {
		var siteid = sid;
		var url = global.contextPath + "/area/getArea?siteid="+siteid
		$("input[name='areaname']").autocomplete({
            source: function( request, response ) {
                $.ajax({
                    url: url,
                    dataType: "json",
                    data:{
                    	areaname: request.term
                    },
                    success: function( data ) {
                        response( $.map( data.datas, function( item ) {
                            return {
                        		label:item.label,
                                value:item.areaname,
                                siteid:item.siteid,
                                province:item.province,
                                city:item.city
                            }
                        }));
                    }
                });
            },
            minLength: 1,
            select: function( event, ui ) {
            	$("input[name='areaname']").val(ui.item.areaname);
    			$("input[name='siteid']").removeAttr("readonly")
    			$("input[name='siteid']").val(ui.item.siteid);
    			$("input[name='siteid']").attr("readonly","readonly");
    			$("input[name='province']").val(ui.item.prov);
    			$("input[name='city']").val(ui.item.city);            		
            }
        });	
	}
    /**
     * 添加区域地图
     */    
	this.addAreaMap = function(){		
		XF.lightSideBar("addAreaMap");
		var url  = global.contextPath + "/area/addMap"
		$.ajax({
		  url: url,
		  type: "post",
		  data: {},
		  complete: function(xhr){
			  xhr = xhr['responseText'];
			  $("#areaCenter").html(xhr);
		  }
		});
	};
	/**
     * 编辑站点区域地图
     */    
	this.editMapSite = function(areaid){
		var url  = global.contextPath + "/area/editMapSite"
		$.ajax({
		  url: url,
		  type: "post",
		  data: {areaid:areaid},
		  complete: function(xhr){
			  xhr = xhr['responseText'];
			  $("#areaCenter").html(xhr);
		  }
		});
	};
	/**
     * 编辑全局区域地图
     */    
	this.editMapCore = function(areaid){
		var url  = global.contextPath + "/area/editMapCore"
		$.ajax({
		  url: url,
		  type: "post",
		  data: {areaid:areaid},
		  complete: function(xhr){
			  xhr = xhr['responseText'];
			  $("#areaCenter").html(xhr);
		  }
		});
	};
	/**
	 * 查看分公司待审核地图
	 **/
	this.checksiteMap = function(areaid){
		var url  = global.contextPath + "/area/checkSiteMap"
		$.ajax({
		  url: url,
		  type: "post",
		  data: {areaid:areaid},
		  complete: function(xhr){
			  xhr = xhr['responseText'];
			  $("#areaCenter").html(xhr);
		  }
		});
	};
    	/**
    	 * 删除区域地图
    	 */
    this.doDelMap = function(areaid){
		$.messager.confirm('提示',"确认删除这条记录？",function(r){
		    if (r){
				var url  = global.contextPath + "/area/doDelMap"
				$.ajax({
				  url: url,
				  type: "post",
				  data: {areaid:areaid},
				  complete: function(xhr){
					  xhr = xhr['responseText'];
					  xhr = new Function("return " + xhr);
					  xhr = xhr();
					  xhr = xhr['status'];
					  if(xhr=="SUCCESS"){
						  $.messager.show({
							title:'提示',
							msg:'删除成功',
							timeout:3000,
							showType:'slide'
						  });
						  $(".userListWrap tr i[s="+areaid+"]").parent().parent().parent().remove();
					  }else if(xhr=="FAILD"){
						  $.messager.show({
							title:'提示',
							msg:'删除失败!',
							timeout:5000,
							showType:'slide'
						  });
					  }
				   }
				});
		    }
		});
	};
	/**
	 * 刷新
	 **/
	this.refreshAuditingMap = function(){
		var url  = global.contextPath + "/area/refreshAuditingMap"
		$.ajax({
		  url: url,
		  type: "post",
		  data: {},
		  complete: function(xhr){
			  xhr = xhr['responseText'];
			  $("#areaCenter").html(xhr);
		  }
		});
	}
	/**
	 * 审核
	 * */
	this.AuditedMap = function(areaid,areaname,siteid){
		var url = global.contextPath + "/area/doAuditingMap"
		
		$.ajax({
			url: url,
			type: "post",
			data: {areaid:areaid,areaname:areaname,siteid:siteid},
			complete: function(xhr){
				xhr = xhr['responseText'];
  			  	$("#areaCenter").html(xhr);
			}
		});
	};
	/**
	 *删除待审核地图围栏
	 *
	 **/
	   this.delAuditingMap = function(areaid){
			$.messager.confirm('提示',"确认删除这条记录？",function(r){
			    if (r){
					var url  = global.contextPath + "/area/delAuditingMap"
					$.ajax({
					  url: url,
					  type: "post",
					  data: {areaid:areaid},
					  complete: function(xhr){
						  xhr = xhr['responseText'];
						  xhr = new Function("return " + xhr);
						  xhr = xhr();
						  xhr = xhr['status'];
						  if(xhr=="SUCCESS"){
							  $.messager.show({
								title:'提示',
								msg:'删除成功',
								timeout:3000,
								showType:'slide'
							  });
							  $(".userListWrap tr i[s="+areaid+"]").parent().parent().parent().remove();
						  }else if(xhr=="FAILD"){
							  $.messager.show({
								title:'提示',
								msg:'删除失败!',
								timeout:5000,
								showType:'slide'
							  });
						  }
					   }
					});
			    }
			});
		};
    	/**
    	 * 全局区域地图
    	 */
    	this.listAllAreaMap = function(){
    		XF.lightSideBar("listAllAreaMap");
    		_areaMap_search('APPOINTTIME','DESC',null,null,null,null);
    	}
    	/**
    	 * 待审核区域地图
    	 */
    	this.auditingAreaMap = function(){
    		XF.lightSideBar("auditingAreaMap");
    		var url  = global.contextPath + "/area/auditingAreaMap"
    		$.ajax({
    		  url: url,
    		  type: "post",
    		  data: {},
    		  complete: function(xhr){
    			  xhr = xhr['responseText'];
    			  $("#areaCenter").html(xhr);
    		  }
    		});
    	};
    	/**
    	 * 站点区域地图
    	 */
    	this.listAreaMap = function(siteid){
    		XF.lightSideBar("listAreaMap");
    		var siteid = siteid + "";
    		_areaMap_search('ENTERDATE','DESC',siteid,null,null,null);
    	}
    	
    	/**
    	 * 区域地图通用调用方法
    	 */
    	this.listAreaMapSearch = function(){
    		var orderByField = 'ENTERDATE';
    		var orderByMode = 'DESC';
    		var siteid = null;
    		var areaname = null;
    		var province = null;
    		var city = null;
    		province = $('#prov').combobox('getValue');
    		city = $('#city').combobox('getValue');
    		siteid = $("input[name=siteid]").val();
    		areaname = $("input[name=areaname]").val();
    		if(typeof(orderByFieldReq)!='undefined' && orderByFieldReq!=""){
    			orderByField = orderByFieldReq;
    			var td = $("th[orderByField="+orderByField+"]");
    			orderByMode = td.find("span.px").attr("orderByMode");
    		}
    		_areaMap_search(orderByField,orderByMode,siteid, areaname,province,city);
    	};
    	/**
    	 * 地图具体搜索方法实现
    	 */
    	 function _areaMap_search(orderByField,orderByMode,siteid,areaname,province,city){     		
    	    	config.areaMap_search['args'] = [orderByField,orderByMode,siteid, areaname,province,city];
    	    	SMT.xconfig.gvar.pageNum = '1';
    	    	SMT.ajax(config.areaMap_search);
    	    }
    
}();


config.area_search = {
	url: ""+global.contextPath+"/area/list",
    argsOrder: ['orderByField','orderByMode','siteId','siteName','areaId','areaName','fldidd','fldsitenames'],
    needVar: true,
    replaceExp: '#areaCenter',
    callBack: function() {
    }		
};
config.areaMap_search = {
		url: ""+global.contextPath+"/area/listMap",
	    argsOrder: ['orderByField','orderByMode','siteid','areaname','province','city'],
	    needVar: true,
	    replaceExp: '#areaCenter',
	    callBack: function() {
	    }		
};